package com.itheima.d1_reflect_field;

import org.junit.jupiter.api.Test;

import java.lang.reflect.Field;

/**
 * 对成员变量进行赋值
 */
public class demo2 {
    @Test
    public void getDeclareField() throws NoSuchFieldException, IllegalAccessException {
        //1.获取类对象
        Class c = Student.class;

        //2.获取单个成员变量
        Field f = c.getDeclaredField("name");
        f.setAccessible(true);
        //3.赋值
        Student s  = new Student();
        f.set(s,"hr");
        System.out.println(s);

        //4.取值
        String s1 = (String) f.get(s);
        System.out.println(s1);
    }


}
